package com.ding.dailyup.service;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.ding.dailyup.util.LoggerUtil;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class RiskService {
	private static final List<String> FORBID_LIST = Lists.newArrayList("成人用品",
			"rmb", "RMB", "傻逼");
	@Autowired
	private WxMaService wxMaService;

	public boolean isRisky(String content) {
		try {
			log.info("开始审核文本:{}", content);
			if (StrUtil.isBlank(content)) {
				return false;
			}
			for (String forbidWord : FORBID_LIST) {
				if (content.contains(forbidWord)) {
					return true;
				}
			}
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("content", content);
			String result = wxMaService.post(
					"https://api.weixin.qq.com/wxa/msg_sec_check",
					jsonObject.toJSONString());
			log.info("审核文本:{},结果:{}", content, result);
		} catch (WxErrorException e) {
			if (e.getError().getErrorCode() == 87014) {
				log.info("审核文本:{},结果有风险", content);
				LoggerUtil.info("isRisky content = {}", content);
				return true;
			}
			LoggerUtil.error("isRisky wx fail", e);
			return true;
		} catch (Exception e) {
			LoggerUtil.error("isRisky fail", e);
			return false;
		}
		return false;
	}
}
